package org.chen.controller;

import lombok.extern.slf4j.Slf4j;
import org.chen.model.dto.ArticleBatchIdsDTO;
import org.chen.model.dto.ArticleBatchSortDTO;
import org.chen.model.dto.ArticleBatchSourceDTO;
import org.chen.model.dto.ArticleBatchStatusDTO;
import org.chen.model.elastic.AdminArticleQueryParam;
import org.chen.model.elastic.ArticlePageResult;
import org.chen.response.Result;
import org.chen.service.CrawlerService;
import org.chen.service.impl.AdminArticleServiceImpl;
import org.chen.service.impl.ArticleSortService;
import org.chen.service.impl.CrawlerServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/article")
public class CrawlerArticleController {

    private final CrawlerServiceImpl service;
    private final AdminArticleServiceImpl adminArticleService;
    private final ArticleSortService sortService;

    public CrawlerArticleController(
                             CrawlerServiceImpl service,
                             AdminArticleServiceImpl adminArticleService,
                             ArticleSortService sortService) {
        this.service = service;
        this.adminArticleService = adminArticleService;
        this.sortService = sortService;
    }

    @GetMapping("/articles")
    public Result<ArticlePageResult> getArticles(
            AdminArticleQueryParam queryParam) {
        ArticlePageResult result = service.getArticles(queryParam);
        return Result.success(result);
    }

    @DeleteMapping("/articles/{id}")
    public Result<Void> deleteArticle(@PathVariable String id) {
        service.deleteArticle(id);
        return Result.success();
    }

    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable String id, @RequestBody Map<String,Integer> status) {
        adminArticleService.updateStatus(id, status.get("status"));
        return Result.success();
    }

    @PutMapping("/{id}/top")
    public Result updateTopStatus(@PathVariable String id,
                                  @RequestParam Boolean isTop,
                                  @RequestParam(required = false) Integer topOrder) {
        sortService.updateTopStatus(id, isTop, topOrder);
        return Result.success();
    }

    @PutMapping("/{id}/hot")
    public Result updateHotStatus(@PathVariable String id,
                                  @RequestParam Boolean isHot,
                                  @RequestParam(required = false) Integer hotOrder) {
        sortService.updateHotStatus(id, isHot, hotOrder);
        return Result.success();
    }

    @PutMapping("/{id}/recommend")
    public Result updateRecommendStatus(@PathVariable String id,
                                        @RequestParam Boolean isRecommend,
                                        @RequestParam(required = false) Integer recommendOrder) {
        sortService.updateRecommendStatus(id, isRecommend, recommendOrder);
        return Result.success();
    }

    @PutMapping("/batch/source")
    public Result<Void> updateArticlesSource(@RequestBody @Validated ArticleBatchSourceDTO dto) {
        log.info("Batch update articles source: {}", dto);
        adminArticleService.updateArticlesSource(dto.getIds(), dto.getSource());
        return Result.success();
    }

    @PutMapping("/batch/status")
    public Result<Void> updateArticlesStatus(@RequestBody @Validated ArticleBatchStatusDTO dto) {
        log.info("Batch update articles status: {}", dto);
        adminArticleService.updateArticlesStatus(dto.getIds(), dto.getStatus());
        return Result.success();
    }

    @DeleteMapping("/batch")
    public Result<Void> deleteArticles(@RequestBody @Validated ArticleBatchIdsDTO dto) {
        log.info("Batch delete articles: {}", dto);
        adminArticleService.deleteArticles(dto.getIds());
        return Result.success();
    }

    // 批量更新置顶状态
    @PutMapping("/batch/top")
    public Result<Void> batchUpdateTop(@RequestBody @Validated ArticleBatchSortDTO dto) {
        log.info("Batch update articles top status: {}", dto);
        sortService.batchUpdateTopStatus(dto.getIds(), dto.getIsTop(), dto.getTopOrder());
        return Result.success();
    }

    // 批量更新热门状态
    @PutMapping("/batch/hot")
    public Result<Void> batchUpdateHot(@RequestBody @Validated ArticleBatchSortDTO dto) {
        log.info("Batch update articles hot status: {}", dto);
        sortService.batchUpdateHotStatus(dto.getIds(), dto.getIsHot(), dto.getHotOrder());
        return Result.success();
    }

    // 批量更新推荐状态
    @PutMapping("/batch/recommend")
    public Result<Void> batchUpdateRecommend(@RequestBody @Validated ArticleBatchSortDTO dto) {
        log.info("Batch update articles recommend status: {}", dto);
        sortService.batchUpdateRecommendStatus(dto.getIds(), dto.getIsRecommend(), dto.getRecommendOrder());
        return Result.success();
    }
//
//    @PutMapping("/articles/{id}/publish")
//    public Result<Void> publishArticle(@PathVariable String id) {
//        service.publishArticle(id);
//        return Result.success();
//    }
//
//    @GetMapping("/images")
//    public Result<Page<CrawlerImageVO>> getImages(
//            @RequestParam(defaultValue = "1") Integer page,
//            @RequestParam(defaultValue = "10") Integer pageSize) {
//        return Result.success(service.getImages(page, pageSize));
//    }
//
//    @DeleteMapping("/images/{id}")
//    public Result<Void> deleteImage(@PathVariable String id) {
//        service.deleteImage(id);
//        return Result.success();
//    }
}
